package daos;

import daos.exceptions.NonexistentEntityException;
import daos.exceptions.PreexistingEntityException;
import entity.EntityUser;
import facade.DaosFactory;
import java.util.List;
import javax.persistence.*;
import vo.UserVo;

public class EntityUserJpaController implements DaosFactory{

    public EntityUserJpaController() {
        emf = Persistence.createEntityManagerFactory("AppServicePU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(EntityUser entityUser) throws PreexistingEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(entityUser);
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findEntityUser(entityUser.getId()) != null) {
                throw new PreexistingEntityException("EntityUser " + entityUser + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(EntityUser entityUser) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            entityUser = em.merge(entityUser);
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                int id = entityUser.getId();
                if (findEntityUser(id) == null) {
                    throw new NonexistentEntityException("The entityUser with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(int id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            EntityUser entityUser;
            try {
                entityUser = em.getReference(EntityUser.class, id);
                entityUser.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The entityUser with id " + id + " no longer exists.", enfe);
            }
            em.remove(entityUser);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public EntityUser findEntityUser(int id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(EntityUser.class, id);
        } finally {
            em.close();
        }
    }

    public int getEntityUserCount() {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select count(o) from User as o");
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

    public List<EntityUser> getUsers(EntityManager em) {
        try {
            Query q = em.createQuery("SELECT u FROM EntityUser as u");
            return q.getResultList();
        } catch (Exception e) {
            System.out.println(e);
            return null;
        }
        finally {            
            em.close();        
        }
    }
}
